package com.csp.base.service.impl;

import java.util.List;

import org.quartz.CronScheduleBuilder;
import org.quartz.CronTrigger;
import org.quartz.Job;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.JobKey;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.TriggerBuilder;
import org.quartz.TriggerKey;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.csp.base.constant.JOB_STATUS;
import com.csp.base.mapper.JobAndTriggerMapper;
import com.csp.base.model.JobAndTrigger;
import com.csp.base.service.IJobAndTriggerService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;


@Service
public class JobAndTriggerImpl implements IJobAndTriggerService{
	@Autowired
	@Qualifier("Scheduler")
	private Scheduler scheduler;

	@Autowired
	private JobAndTriggerMapper jobAndTriggerMapper;
	
	public PageInfo<JobAndTrigger> getJobAndTriggerDetails(int pageNum, int pageSize) {
		PageHelper.startPage(pageNum, pageSize);
		List<JobAndTrigger> list = jobAndTriggerMapper.getJobAndTriggerDetails();
		PageInfo<JobAndTrigger> page = new PageInfo<JobAndTrigger>(list);
		return page;
	}

	@Override
	public void updateJobStatusToStopping(String jobName, String jobGroup) {
		jobAndTriggerMapper.updateJobStatusByJobName$JobGroup(jobName,jobGroup,JOB_STATUS.STOPPING);
	}

	@Override
	public void updateJobStatusToRunning(String jobName, String jobGroup) {
		jobAndTriggerMapper.updateJobStatusByJobName$JobGroup(jobName,jobGroup,JOB_STATUS.RUNNING);
	}

	@Override
	public void addJob(String jobName, String jobClassName, String jobGroupName, String cronExpression) {
		JobDetail jobDetail = JobBuilder.newJob(getClass(jobClassName).getClass()).withIdentity(jobName, jobGroupName).build();
		CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
		CronTrigger trigger = TriggerBuilder.newTrigger().withIdentity(jobName, jobGroupName).withSchedule(scheduleBuilder).build();
		try {
			scheduler.scheduleJob(jobDetail, trigger);
			scheduler.start();
		} catch (SchedulerException e) {
			//TODO
			//throw new CodeException("xxx","XXX");
		}
	}
	
	@Override
	public void jobdelete(String jobClassName, String jobGroupName)  {
		try {
			scheduler.pauseTrigger(TriggerKey.triggerKey(jobClassName, jobGroupName));
			scheduler.unscheduleJob(TriggerKey.triggerKey(jobClassName, jobGroupName));
			scheduler.deleteJob(JobKey.jobKey(jobClassName, jobGroupName));
		} catch (SchedulerException e) {
			//TODO
			//throw new CodeException("xxx","XXX");
		}
	}

	@Override
	public void jobReschedule(String jobName, String jobGroupName, String cronExpression) {
		try {
			TriggerKey triggerKey = TriggerKey.triggerKey(jobName, jobGroupName);
			CronScheduleBuilder scheduleBuilder = CronScheduleBuilder.cronSchedule(cronExpression);
			CronTrigger trigger = (CronTrigger) scheduler.getTrigger(triggerKey);
			trigger = trigger.getTriggerBuilder().withIdentity(triggerKey).withSchedule(scheduleBuilder).build();
			scheduler.rescheduleJob(triggerKey, trigger);
			//JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(jobName, jobGroupName));
			scheduler.pauseJob(JobKey.jobKey(jobName, jobGroupName));
			jobAndTriggerMapper.updateJobStatusByJobName$JobGroup(jobName, jobGroupName, JOB_STATUS.STOPPING);
		} catch (SchedulerException e) {
			//TODO
			//throw new CodeException("xxx","XXX");
		}
	}

	@Override
	public void jobPause(String jobName, String jobGroupName) {
		try {
			//JobDetail jobDetail = scheduler.getJobDetail(JobKey.jobKey(jobName, jobGroupName));
			jobAndTriggerMapper.updateJobStatusByJobName$JobGroup(jobName, jobGroupName, JOB_STATUS.STOPPING);
			scheduler.pauseJob(JobKey.jobKey(jobName, jobGroupName));
		} catch (SchedulerException e) {
			//TODO
			//throw new CodeException("xxx","XXX");
		}
	}

	@Override
	public void jobresume(String jobName, String jobGroupName) {
		try {
			jobAndTriggerMapper.updateJobStatusByJobName$JobGroup(jobName, jobGroupName, JOB_STATUS.RUNNING);
			scheduler.resumeJob(JobKey.jobKey(jobName, jobGroupName));
		} catch (SchedulerException e) {
			//TODO
			//throw new CodeException("xxx","XXX");
		}
	}
	
	/**
	 * 公共方法
	 * @param classname
	 * @return
	 * @throws Exception
	 */
	public Job getClass(String classname) {
		try {
			Class<?> class1 = Class.forName(classname);
			return (Job) class1.newInstance();
		} catch (Exception e) {
			//TODO
			//throw new CodeException("xxx","XXX");
		}
		return null;
	}
}